const jwt = require('../utils/jwt');
const { secret } = require('../config');
const db = require('../mysql/index');

// 登录
exports.login = (req, res, next) => {
    const { username, password } = req.body;
    console.log(username, password);
    const sql1 = 'select * from userInfo where username = ?'
    db.query(sql1, username, (err, result) => {
        if (result.length > 0) {
            let token;
            let identify = result[0].identify
            jwt.sign({ username }, secret, { expiresIn: '24h' }).then((res1) => {
                token = res1;
                password === result[0].password ? res.send({
                    msg: '登录成功',
                    token: token,
                    username: username,
                    identify: identify,
                    code: 200
                }) : res.send({
                    msg: '密码错误',
                    code: 400
                })
            })
        } else {
            res.send({
                msg: '用户名不存在',
                code: 400
            })
        }
    })
}

// 新增用户
exports.addUser = (req, res, next) => {
    const { name, username, password, identify, phone } = req.body;
    const sql1 = 'select * from userInfo where username = ?'
    db.query(sql1, username, (err, result) => {
        console.log(result, 'resuuuuuuu');
        if (result.length > 0) {
            res.send({
                msg: '用户名已存在',
                code: 400
            })
        } else {
            const sql2 = 'insert into userInfo set ?'
            db.query(sql2, { name: name, username: username, password: password, identify: 2, phone: phone }, (err, result) => {
                if (result) {
                    console.log(result);
                    res.send({
                        msg: '添加成功',
                        code: 200
                    })
                } else {
                    res.send({
                        msg: err,
                        code: 400
                    })
                }
            })
        }
    })
}

// 查询所有用户
exports.getUserList = (req, res, next) => {
    const sql1 = 'select * from userInfo'
    db.query(sql1, (err, result) => {
        if (result) {
            res.send({
                msg: '查询成功',
                code: 200,
                data: result
            })
        } else {
            res.send({
                msg: err,
                code: 400
            })
        }
    })

}

// 修改用户权限
exports.changeIdentify = (req, res, next) => {
    const { id, identify } = req.body;
    const sql1 = 'update userInfo set identify = ? where id = ?'
    db.query(sql1, [identify, id], (err, result) => {
        console.log(result, 'result');
        if (result) {
            res.send({
                msg: '修改成功',
                code: 200
            })
        } else {
            res.send({
                msg: err,
                code: 400
            })
        }
    })
}

// 删除用户
exports.deleteUser = (req, res, next) => {
    const { id } = req.body;
    console.log(id, 'iddd');
    const sql1 = 'delete from userInfo where id = ?'
    db.query(sql1, id, (err, result) => {
        if (result) {
            res.send({
                msg: '删除成功',
                code: 200
            })
        } else {
            res.send({
                msg: err,
                code: 400
            })
        }
    })
}

// 获取路由列表
exports.getRouterList = (req, res, next) => {
    // const { username, identify } = req.body;
    // if (!username && !identify) {
    //     const sql3 = 'select * from routes'
    //     db.query(sql3, (err, result) => {
    //         let result1 = result.map((item) => {
    //             delete item.identify
    //             // delete item.id
    //             return item
    //         })
    // let result2 = []
    // result1.map((item) => {
    //     // 数组去重
    //     result2.push(item)
    //     if (result2.length >= 2 && result2[result2.length - 1].path === result2[result2.length - 2].path) {
    //         result2.pop(result2[result2.length - 1])
    //     }
    // })
    //         if (result) {
    //             res.send({
    //                 msg: '查询成功',
    //                 code: 200,
    //                 data: result2
    //             })
    //         }
    //     })
    // }
    const sql1 = 'select * from routes'
    db.query(sql1, (err, result) => {
        if (result) {
            res.send({
                msg: '查询成功',
                code: 200,
                data: result
            })
        } else {
            res.send({
                msg: err,
                code: 400
            })
        }
    })
}

// 添加路由列表
exports.addRouter = (req, res, next) => {
    const { path, component, label } = req.body
    const sql2 = 'select * from routes where path = ?'
    db.query(sql2, path, (err, result) => {
        if (result.length > 0) {
            res.send({
                msg: '路由已存在',
                code: 400
            })
        } else {
            const sql1 = 'insert into routes set ?'
            db.query(sql1, { path: path, component: component, label: label }, (err, result) => {
                if (result) {
                    res.send({
                        msg: '添加成功',
                        code: 200
                    })
                } else {
                    res.send({
                        msg: err,
                        code: 400
                    })
                }
            })
        }
    })
}





// 获取身份列表或者根据身份获得对应路由
exports.getIdentifyList = (req, res, next) => {
    const { identify } = req.body
    console.log(identify, 'identify');
    if (identify) {
        const sql2 = 'select * from userIdentify where identify = ?'
        db.query(sql2, identify, (err, result) => {
            if (result) {
                result.map((item) => {
                    item.routes = JSON.parse(item.routes)
                })
                // console.log(result, 'result');
                res.send({
                    msg: '查询成功',
                    code: 200,
                    data: result[0].routes
                })
            } else {
                res.send({
                    msg: err,
                })
            }
        })
    } else {
        const sql1 = 'select * from userIdentify'
        db.query(sql1, (err, result) => {
            if (result) {
                result.map((item) => {
                    item.routes = JSON.parse(item.routes)
                })
                res.send({
                    msg: '查询成功',
                    code: 200,
                    data: result
                })
            } else {
                res.send({
                    msg: err,
                })
            }
        })
    }
}

// 更新身份的路由
exports.updateIdentifyList = (req, res, next) => {
    const { identify, routes } = req.body;
    const sql1 = 'update userIdentify set routes = ? where identify = ?'
    db.query(sql1, [JSON.stringify(routes), identify], (err, result) => {
        if (result) {
            res.send({
                msg: '修改成功',
                code: 200
            })
        }
    })
}

exports.updateIdentifyRouter = (req, res, next) => {
    const { identify, routeLabel } = req.body;
    // console.log(identify, routeLabel, 'identify, routeLabel');
    const sql1 = 'select * from userIdentify where identify = ?'
    db.query(sql1, identify, (err, result) => {
        // console.log(result[0]);
        let arr1 = JSON.parse(result[0].routes)
        console.log(arr1, '本来就有的路由列表');
        let sql2 = 'select * from routes'
        db.query(sql2, (err, result1) => {
            // 有三个内容：1. 路由列表 2. 当前身份拥有的路由 3. 选择的路由
            let arr3 = []
            result1.forEach((item) => {
                if (routeLabel.includes(item.label)) {
                    // console.log(item, 'item');
                    delete item.id
                    arr3.push(item)
                }
            })
            console.log(arr3, '修改完的路由列表');
            // console.log(arr1, 'arr1和arr2');
            // let arr2 = []
            // arr1.map((item) => {
            //     // 数组去重
            //     arr2.push(item)
            //     if (arr2.length >= 2 && arr2[arr2.length - 1].path === arr2[arr2.length - 2].path) {
            //         arr2.pop(arr2[arr2.length - 1])
            //     }
            // })
            // console.log(arr2,'去重后的数组');
            // 重新转为 字符串
            // 调用方法更新
            const sql3 = 'update userIdentify set routes = ? where identify = ?'
            db.query(sql3, [JSON.stringify(arr3), identify], (err, result) => {
                if (result) {
                    res.send({
                        msg: '修改成功',
                        code: 200
                    })
                } else {
                    res.send({
                        msg: err,
                        code: 400
                    })
                }
            })
        })
    })
}

// 创建登录日志
exports.addLoginLog = (req, res, next) => {
    const { username, identify, ip, address, loginTime } = req.body
    const sql1 = 'insert into loginLog set ?'
    db.query(sql1, { username: username, identify: identify, ip: ip, address: address, loginTime: loginTime }, (err, result) => {
        if (result) {
            res.send({
                msg: '添加成功',
                code:200
            })
        }
    })
}
// 查询登录日志
exports.getLoginLog = (req, res, next) => {
    const { username } = req.query
    const sql1 = 'select * from loginLog where username = ?'
    db.query(sql1, username,  (err, result) => {
        if (result) {
            res.send({
                msg: '查询成功',
                code: 200,
                data: result
            })
        }
    })
}